package com.gua.jbs.daoImpl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.gua.jbs.dao.ICouponDao;
import com.gua.jbs.pojo.Jbscdt;
import com.gua.jbs.utility.ApplicationException;
import com.gua.jbs.utility.DBConstant;
import com.gua.jbs.utility.ExceptionMessage;
import com.gua.jbs.utility.QueryConstant;

public class CouponDao extends BaseDao implements ICouponDao {

	@Override
	public boolean saveCoupon(Jbscdt jbscdt) throws ApplicationException {
		// TODO Auto-generated method stub
		// Prepare the parameters
		List lParamList = new ArrayList();
		boolean isSaved = false;
		int pk = 0;
		if (jbscdt != null) {
			// set the parameters
			try {
				pk = getPrimaryKey("PROD_ID", "JBSPIT");
				lParamList.add(0, pk);
				lParamList.add(1, jbscdt.getCodeName());
				lParamList.add(2, jbscdt.getCodeValue());
				lParamList.add(3, jbscdt.getCodeGroup());
				lParamList.add(4, DBConstant.IS_ACTIVE_YES);
				lParamList.add(5, new Timestamp(new Date().getTime()));
				lParamList.add(6, new Timestamp(new Date().getTime()));
				lParamList.add(7, jbscdt.getExtrafield1());
				lParamList.add(8, jbscdt.getExtrafield2());
				lParamList.add(9, null);
				lParamList.add(10, "Yogesh");
				lParamList.add(11, "Yogesh");

				// Get the query by passing parameters
				String query = getQuery(QueryConstant.SAVE_COUPON, lParamList);
				int resultCount = 0;
				resultCount = insert(query);
				if (resultCount != 0)
					isSaved = true;
				else
					isSaved = false;
			} catch (SQLException sqe) {
				throw new ApplicationException(
						ExceptionMessage.COUPON_SAVE_UNABLE);
			}
		}
		return isSaved;

	}

	public Map<Integer, Map<String, String>> getCuoponTypes()
			throws ApplicationException {
		// TODO Auto-generated method stub

		Map<Integer, Map<String, String>> couponTypes = new LinkedHashMap<Integer, Map<String, String>>();
		List lParamList = new ArrayList();
		lParamList.add(0, DBConstant.CODE_GROUP_COUPON);

		String query = getQuery(QueryConstant.GET_COUPON_TYPES, lParamList);
		try {
			ResultSet rs = getResults(query);
			while (rs.next()) {
				String prodCatName = rs.getString("CODE_NAME");
				String prodCatCode = rs.getString("CODE_VALUE");
				Map<String, String> categoryMap = new LinkedHashMap<String, String>();
				categoryMap.put(prodCatName, prodCatCode);
				couponTypes.put(rs.getInt("CODE_ID"), categoryMap);
			}

		} catch (SQLException sqe) {
			throw new ApplicationException(ExceptionMessage.COUPON_TYPE_UNABLE);
		}
		return couponTypes;
	}

	/**
	 * Method to get coupon information
	 * 
	 * @param couponCode
	 * @return
	 * @throws ApplicationException
	 */
	public Jbscdt getCouponInfoByCode(String couponCode)
			throws ApplicationException {
		Jbscdt jbscdt = new Jbscdt();
		List lParamList = new ArrayList();
		lParamList.add(0, DBConstant.CODE_GROUP_COUPON);
		lParamList.add(1, couponCode);
		lParamList.add(2, DBConstant.IS_ACTIVE_YES);
		String query = getQuery(QueryConstant.GET_COUPON_BY_CODE, lParamList);
		ResultSet rs = null;
		int stock = 0;
		try {
			rs = getResults(query);
			if (rs != null) {
				while (rs.next()) {
					jbscdt.setCodeId(String.valueOf(rs.getInt("PROD_ID")));
					jbscdt.setCodeValue(rs.getString("CODE_VALUE"));
					jbscdt.setExtrafield1(rs.getString("EXTRA_FIELD1"));
					jbscdt.setExtrafield2(rs.getString("EXTRA_FIELD2"));
					jbscdt.setExtrafield3(rs.getString("EXTRA_FIELD3"));
				}
			}
		} catch (SQLException sqe) {
			throw new ApplicationException(ExceptionMessage.GET_COUPON_UNABLE);
		}
		return jbscdt;
	}

}
